<?php
/**
 * 业主controll基类,不需要登录时继承该类
 * @author Administrator
 *
 */
class CControllerOwnerBase extends CControllerBase{
	
	protected $_ownerID = 0;
	
	protected $_ownerName = "";
	
	protected $_ownerInfo = array();
	
	protected $_cityName = "";
	
	function __construct($controller, $action){
		parent::__construct($controller, $action);
		$this->_cityName="杭州市";

		if(isset($_COOKIE["city_name"]))
		{
			$this->_cityName = $_COOKIE["city_name"];
		}
		
	}
	
	public function render($viewFile,$param=array(),$masterView="common/master_nologin")
	{
		if(!$this->_ownerID)
		{
			$this->isLogin();
		}

		$this->_cityName = str_replace("市","站",$this->_cityName);
		if(mb_strlen($this->_cityName,"utf-8")>4)
		{
			$this->_cityName = mb_substr($this->_cityName,0,4,"utf-8");
		}

		
		$this->setViewParam("city_name",$this->_cityName);
		$this->setViewParam("ownerID",$this->_ownerID);
		$this->setViewParam("ownerName", $this->_ownerName);
		if($masterView)
		{
			$ext = Config::get("viewSet.ext");
			$this->setViewParam("content",$viewFile.$ext);
			parent::render($masterView,$param);
		}
		else
		{
			parent::render($viewFile,$param);
		}
	}
	
	protected function isLogin()
	{
		if(isset($_SESSION["ownerInfo"]))
		{
			$this->_ownerID = $_SESSION["ownerInfo"]["owner_id"];
			$this->_ownerName = $_SESSION["ownerInfo"]["owner_name"];
			
			$this->getOwnerInfo();
			return true;
		}
		return false;
	}
	
	protected function getOwnerInfo()
	{
		if($this->_ownerInfo)
		{
			return;
		}
		
		$oModel = new ownerModel();
		
		$oInfo = $oModel->selectByWhere("t_owner o,t_community c",
				"c.province,c.city,c.area,c.community_name,o.card_status,o.pay_status,o.balance",
				array(),"o.community_id=c.community_id and o.del_flag=0 and o.owner_id=".$this->_ownerID);
		
		if(!$oInfo)
		{
			$this->logout();
		}
		
		$this->_ownerInfo = $oInfo;
		
		$this->setViewParam("oInfo", $this->_ownerInfo);
	}
	
	/**
	 * 退出
	 */
	protected function logout()
	{
		unset($_SESSION["ownerInfo"]);
		$this->redirectUrl("/");
	}
	
	/**
	 * 跳转到提示页面
	 * @param string $msgDetail 提示具体内容
	 * @param int $msgType 消息类型 0成功,1错误,2警告
	 * @param array $links 按钮链接和文字数组 ，格式array(array("text"=>"","href"=>""),array..)，如果为空，则自动显示一个“返回上一页”按钮,
	 * @param bool $auto_redirect 是否倒计时自动跳转
	 */
	protected function sysMsg($msgDetail, $msgType = 0, $links = array(), $autoRedirect = false)
	{
		$this->_validator = null;
		//$this->setMenu("信息提示");
	    if (count($links) == 0)
	    {
	        $links[0]['text'] = "返回上一页";
	        $links[0]['href'] = 'javascript:history.go(-1)';
	    }

	    $array=array('ur_here'=>"系统消息", 'msg_detail'=>$msgDetail, 'msg_type'=>$msgType,
	    	'links'=>$links,'default_url'=>$links[0]['href'],'auto_redirect'=>$autoRedirect);

	    $this->render("common/message",$array);
	    exit;
	}

}